package com.soda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.soda.bean.Course;
import com.soda.bean.Information;
import com.soda.bean.Notice;
import com.soda.service.CourseServiceHan;
import com.soda.service.InformationServiceHan;
import com.soda.service.NoticeService;
import com.soda.service.impl.CourseServiceHanImpl;
import com.soda.service.impl.InformationServiceHanImpl;
import com.soda.service.impl.NoticeServiceImpl;
import com.soda.util.PageInfo;

public class ShowQIanTaiServletHan extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		CourseServiceHan csh = new CourseServiceHanImpl();
		InformationServiceHan is = new InformationServiceHanImpl();
		NoticeService ns= new NoticeServiceImpl();

		HttpSession session = request.getSession();
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String met = request.getParameter("met");
		String couname=null;
		
		if (met==null){
			
			List<Information> list=is.getSelectAll();
			request.setAttribute("informations", list);
			
			List<Course> list1 = csh.getAllSelect();
			request.setAttribute("course", list1);
			
			List<Notice> notice = ns.getSelectAll();
			request.setAttribute("norices",notice);
			
			request.getRequestDispatcher("qiantai/index.jsp").forward(request, response);
			
			
		}
		
		if("xianchang".equals(met)){
			
			//request.setCharacterEncoding("utf-8");
			String couname1=request.getParameter("couName");
			couname1=new String(couname1.getBytes("iso-8859-1"),"utf-8");
			PageInfo pi = new PageInfo(1, 4, 0, 0);
			if(couname1!=null){
				couname = couname1;
			}			
			int condiTotal = csh.selectTotalCount(couname);
			pi.setTotal(condiTotal);
			String num=request.getParameter("page");
			if (num!= null) {
				pi.setPage(Integer.parseInt(num));
			}
			List<Course> list =csh.getCouNameSelect(couname,pi.getPage(),pi.getPer());
			request.setAttribute("course1", list);
			request.setAttribute("page", pi);
			request.getRequestDispatcher("qiantai/xianchang.jsp").forward(request, response);

		}
		if("baoming".equals(met)){
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			PageInfo pi = new PageInfo(1, 4, 0, 0);  
			
			 String[] couid={"0"};
			 String couids = request.getParameter("Array");
			 System.out.println(couids);
			 if(couids!=null)
			 couid = couids.split(",");
			

			List<Course> list = new ArrayList<Course>();
			if (session.getAttribute("course") != null) {
				list = (List<Course>) session.getAttribute("course");
			}
			List<Integer> gids = csh.getIdByChar(); 
			abc:for (int i = 0; i < couid.length; i++) {
				int flag = 1;
				int id=0; 
				id=Integer.parseInt(couid[i]);
				for (int z = 0; z < gids.size(); z++) {
					if (gids.get(z) == id) {
						flag = 0;
						out.print("<script>alert('注意:你的未结算课程里面有此项!');</script>");
						continue abc;
					}
				}
				if (flag == 1) {
					int insert =csh.getInsert(id);	
					
				}	
			}		
			int condiTotal = csh.selectTotalCount();
			pi.setTotal(condiTotal);
			
			String num=request.getParameter("page");
			if (num!= null) {
				pi.setPage(Integer.parseInt(num));
			}
			List<Course> cous = csh.getAllcartSelect(pi.getPage(),pi.getPer());
			
			//System.out.println(cous);
			
            request.setAttribute("ids", couids);
			request.setAttribute("course",cous);
			request.setAttribute("page", pi);
			request.getRequestDispatcher("qiantai/pay.jsp").forward(request, response);

		}
		if(("xiangxi").equals(met)){
			
			int couid=Integer.parseInt(request.getParameter("couid"));
			
			Course course = csh.getCouIdcourseinfo(couid);
			request.setAttribute("course", course);
			request.getRequestDispatcher("qiantai/courseInfo.jsp").forward(request, response);
		}
		

		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
